﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obout.Ajax.UI.HTMLEditor;

using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class AnnouncementDetail : BasePermissionPage
{
    KRJEntities ctx = new KRJEntities();
    [WebMethod]
    public static Object getDept(string DEPT_NO)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();

        try
        {
            DataSet ds = null;
            AnnouncementDetail detail = new AnnouncementDetail();
            ds = detail.DBINT(DEPT_NO);
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000" )
            {
                results = m_dba.RowsToDictionary(ds.Tables["DEPT"], ds.Tables["DEPT"].Rows.Count.ToString(), "1");
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no讀取無資料！";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = "error" +BP.BP_Message;
            BP.ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "getDept", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }
    [WebMethod]
    public static Object getCondition(string BULLETIN_ID)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();

        try
        {
            DataSet ds = null;
            AnnouncementDetail detail = new AnnouncementDetail();
            ds = detail.CONDITION(BULLETIN_ID);
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                results = m_dba.RowsToDictionary(ds.Tables["CONDITION"], ds.Tables["CONDITION"].Rows.Count.ToString(), "1");
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error讀取無資料！";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = "error" + BP.BP_Message;
            BP.ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "getCondition", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }

    [WebMethod]
    public static Object SAVE_Condition(string BULLETIN_ID, string[] SEQ, string[] company, string[] bu, string[] zone, string[] AREA, string[] Dept)
    {
        Object results = new Object();
        string sMessage = "", sSEQ = "";
        AnnouncementDetail detail = new AnnouncementDetail();
        try
        {
            for (int i = 0; i < SEQ.Length - 1; i++)
            {
                sSEQ = int.Parse(SEQ[i]).ToString("00");
                sMessage = detail.SAVE_Condition_S(BULLETIN_ID, sSEQ, company[i], bu[i], zone[i], AREA[i], Dept[i], SEQ.Length - 1);
                if (sMessage == "")
                {
                    results = "";
                }
                else
                {
                    results = "error !Condition存檔錯誤！";
                    break;
                }
            }
            
        }
        catch (Exception ex)
        {
           // results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = "error" + BP.BP_Message;
            BP.ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "SAVE_Condition", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }
    
  protected void Page_Load(object sender, EventArgs e)
  {
    string mode = "";
    try
    {
        mode = Request["mode"];
        //語言別初始值

        if (!IsPostBack)
        {
            rdo_TW.Checked = true;
            hid_FLAG.Value = "TW";
            CV.ValueToCompare = DateTime.Now.ToString("yyyyMMdd");
            hidSYSDATE.Value = DateTime.Now.ToString("yyyyMMdd");
            if (Request["bid"] != null)
            {
                string bid = Request["bid"];
                txt_BULLETIN_ID.Text = bid;
                string savepath = Server.MapPath(ConfigurationManager.AppSettings["UploadFilePath"].ToString() + bid);
                FileUtil.GetFilesList(savepath, GVFileLists);
            }

            if (Request["mode"] != null)
            {

                mode = Request["mode"];
                switch (mode)
                {
                    case "add":
                        txt_BULLETIN_ID.Text = DateTime.Now.ToString("yyyyMMdd-HHmmss-ff");
                        txt_START_DATE.Text = DateTime.Now.ToString("yyyyMMdd");
                        InsertMode();
                        break;
                    case "edit":
                        UpdateMode();
                        break;
                    case "view":
                        btn_Insert.Visible = false;
                        btn_Exit.Visible = true;
                        btn_Delete.Visible = false;
                        btn_Update.Visible = false;
                        PanelDetail.Enabled = false;
                        SetHTMLEditorPreviewMode();
                        break;
                    case "delete":
                        btn_Insert.Visible = false;
                        btn_Exit.Visible = true;
                        btn_Delete.Visible = true;
                        btn_Update.Visible = false;
                        PanelDetail.Enabled = false;
                        SetHTMLEditorPreviewMode();
                        break;
                }

                //Show Detail Data
                if (mode != "add")
                {
                    KRJEntities dc = new KRJEntities();
                    var CUR_BULLETIN_MASTER = dc.KRJ_BULLETIN_MASTER
                       .Where(b => b.BULLETIN_ID == txt_BULLETIN_ID.Text)
                       .FirstOrDefault();
                    if (CUR_BULLETIN_MASTER != null)
                    {
                        txt_START_DATE.Text = CUR_BULLETIN_MASTER.START_DATE;
                        if (!string.IsNullOrEmpty(txt_START_DATE.Text))
                        {
                            if (int.Parse(txt_START_DATE.Text) < int.Parse(DateTime.Now.ToString("yyyyMMdd")))
                            {
                                txt_START_DATE.Enabled = false;
                                ImageCal.Visible = false;
                            }
                        }

                        txt_END_DATE.Text = CUR_BULLETIN_MASTER.END_DATE;
                        txt_BULLETIN_TEXT.Text = CUR_BULLETIN_MASTER.BULLETIN_TEXT_TW;
                        txt_BULLETIN_SPEC.Text = CUR_BULLETIN_MASTER.BULLETIN_SPEC_TW == null ? string.Empty : CUR_BULLETIN_MASTER.BULLETIN_SPEC_TW;
                        hid_BULLETIN_TEXT_GB.Value = HttpUtility.HtmlEncode(CUR_BULLETIN_MASTER.BULLETIN_TEXT_GB);
                        hid_BULLETIN_SPEC_GB.Value = HttpUtility.HtmlEncode(CUR_BULLETIN_MASTER.BULLETIN_SPEC_GB == null ? string.Empty : CUR_BULLETIN_MASTER.BULLETIN_SPEC_GB);
                        hid_BULLETIN_TEXT_EN.Value = HttpUtility.HtmlEncode(CUR_BULLETIN_MASTER.BULLETIN_TEXT_EN);
                        hid_BULLETIN_SPEC_EN.Value = HttpUtility.HtmlEncode(CUR_BULLETIN_MASTER.BULLETIN_SPEC_EN == null ? string.Empty : CUR_BULLETIN_MASTER.BULLETIN_SPEC_EN);

                        //txt_BULLETIN_TEXT_GB.Text = CUR_BULLETIN_MASTER.BULLETIN_TEXT_GB;
                        //txt_BULLETIN_SPEC_GB.Text = CUR_BULLETIN_MASTER.BULLETIN_SPEC_GB == null ? string.Empty : CUR_BULLETIN_MASTER.BULLETIN_SPEC_GB;
                        //txt_BULLETIN_TEXT_EN.Text = CUR_BULLETIN_MASTER.BULLETIN_TEXT_EN;
                        //txt_BULLETIN_SPEC_EN.Text = CUR_BULLETIN_MASTER.BULLETIN_SPEC_EN == null ? string.Empty : CUR_BULLETIN_MASTER.BULLETIN_SPEC_EN;
                        txt_REMARK.Text = CUR_BULLETIN_MASTER.REMARK;
                        ddl_Status.Text = CUR_BULLETIN_MASTER.STATUS;
                    }

                }
            }

        }
    }
    catch (Exception ex)
    {
        lbl_Message.Text = BP_Message;
        ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "Page_Load", ex.Message, "", "c#", "0", ex.StackTrace);
    }
    
    ClientScript.RegisterClientScriptBlock(this.GetType(), "getDept", "<script language='javascript'>$(document).ready(function () {getCondition('" + mode + "');getDept(''); }) </script>");
  }

  protected void SetHTMLEditorPreviewMode()
  {
    txt_BULLETIN_SPEC.ShowDesignButton = false;
    txt_BULLETIN_SPEC.ShowPreviewButton = false;
    txt_BULLETIN_SPEC.ActiveModeType = ActiveModeType.Preview;
    //txt_BULLETIN_SPEC_GB.ShowDesignButton = false;
    //txt_BULLETIN_SPEC_GB.ShowPreviewButton = false;
    //txt_BULLETIN_SPEC_GB.ActiveModeType = ActiveModeType.Preview;
    //txt_BULLETIN_SPEC_EN.ShowDesignButton = false;
    //txt_BULLETIN_SPEC_EN.ShowPreviewButton = false;
    //txt_BULLETIN_SPEC_EN.ActiveModeType = ActiveModeType.Preview;
  }

  protected void InsertMode()
  {
    btn_Insert.Visible = true;
    btn_Exit.Visible = true;
    btn_Delete.Visible = false;
    btn_Update.Visible = false;
  }

  protected void UpdateMode()
  {
    btn_Insert.Visible = false;
    btn_Exit.Visible = true;
    btn_Delete.Visible = false;
    btn_Update.Visible = true;
  }

  protected void btn_Exit_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/Announcement/Announcement.aspx");
  }
  protected void btn_Insert_Click(object sender, EventArgs e)
  {
    try
    {
      if (Page.IsValid)
      {
        //if (txt_BULLETIN_TEXT.Text.Length <= 0 && txt_BULLETIN_TEXT_EN.Text.Length <= 0 && txt_BULLETIN_TEXT_GB.Text.Length <= 0)
        //{
        //    CommonUtil.DisplayErrorMessage("請輸入公告大綱", lbl_Message);
        //    return;
        //}
        //if (txt_BULLETIN_SPEC.Text.Length <= 0 && txt_BULLETIN_SPEC_EN.Text.Length <= 0 && txt_BULLETIN_SPEC_GB.Text.Length <= 0)
        //{
        //  CommonUtil.DisplayErrorMessage("請輸入公告內容",lbl_Message);
        //  return;          
        //}
          char[] delimiterChars = { ','};
          string[] seq = hidseq_C.Value.Split(delimiterChars);
          string[] company = hidcompany_C.Value.Split(delimiterChars);
          string[] bu = hidbu_C.Value.Split(delimiterChars);
          string[] zone = hidzone_C.Value.Split(delimiterChars);
          string[] area = hidarea_C.Value.Split(delimiterChars);
          string[] dept = hiddept_C.Value.Split(delimiterChars);
          string sSEQ = "";
          for (int i = 0; i < seq.Length - 1; i++)
          {
              sSEQ = seq[i].ToString();
              sSEQ = int.Parse(sSEQ).ToString("00");
              SAVE_Condition_S(txt_BULLETIN_ID.Text, sSEQ, company[i], bu[i], zone[i], area[i], dept[i], seq.Length - 1);
          }
          string BULLETIN_TEXT_TW = "", BULLETIN_TEXT_GB = "", BULLETIN_TEXT_EN = "";
          string BULLETIN_SPEC_TW = "", BULLETIN_SPEC_GB = "", BULLETIN_SPEC_EN = "";

          BULLETIN_TEXT_TW = HttpUtility.HtmlDecode(hid_BULLETIN_TEXT_TW.Value.Trim());
          BULLETIN_SPEC_TW = HttpUtility.HtmlDecode(hid_BULLETIN_SPEC_TW.Value.Trim());
          BULLETIN_TEXT_GB = HttpUtility.HtmlDecode(hid_BULLETIN_TEXT_GB.Value.Trim());
          BULLETIN_SPEC_GB = HttpUtility.HtmlDecode(hid_BULLETIN_SPEC_GB.Value.Trim());
          BULLETIN_TEXT_EN = HttpUtility.HtmlDecode(hid_BULLETIN_TEXT_EN.Value.Trim());
          BULLETIN_SPEC_EN = HttpUtility.HtmlDecode(hid_BULLETIN_SPEC_EN.Value.Trim());

          

        //新增EDM資料至DB
        //KRJEntities dc = new KRJEntities();
        KRJ_BULLETIN_MASTER NEW_BULLETIN_MASTER = new KRJ_BULLETIN_MASTER();
        NEW_BULLETIN_MASTER.BULLETIN_ID = txt_BULLETIN_ID.Text;
        NEW_BULLETIN_MASTER.START_DATE = txt_START_DATE.Text;
        NEW_BULLETIN_MASTER.END_DATE = string.IsNullOrEmpty(txt_END_DATE.Text) ? "20201231" : txt_END_DATE.Text; //假設沒有給結束日期，則指定2020年

        NEW_BULLETIN_MASTER.BULLETIN_TEXT_TW = BULLETIN_TEXT_TW;
        NEW_BULLETIN_MASTER.BULLETIN_SPEC_TW = BULLETIN_SPEC_TW;
        NEW_BULLETIN_MASTER.BULLETIN_TEXT_GB = BULLETIN_TEXT_GB;
        NEW_BULLETIN_MASTER.BULLETIN_SPEC_GB = BULLETIN_SPEC_GB;
        NEW_BULLETIN_MASTER.BULLETIN_TEXT_EN = BULLETIN_TEXT_EN;
        NEW_BULLETIN_MASTER.BULLETIN_SPEC_EN = BULLETIN_SPEC_EN;
        NEW_BULLETIN_MASTER.REMARK = txt_REMARK.Text;
        NEW_BULLETIN_MASTER.STATUS = ddl_Status.SelectedValue;
        //紀錄建立時間
        NEW_BULLETIN_MASTER.CRT_ID = User.Identity.Name;
        NEW_BULLETIN_MASTER.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
        NEW_BULLETIN_MASTER.CRT_TIME = DateTime.Now.ToString("HHmmss");

        ctx.KRJ_BULLETIN_MASTER.Add(NEW_BULLETIN_MASTER);
        ctx.SaveChanges();

        //當新增時，同步上傳檔案 FileUpload_Attachments            
        string savepath =
            Server.MapPath(ConfigurationManager.AppSettings["UploadFilePath"].ToString() + txt_BULLETIN_ID.Text);
        FileUtil.UploadMultiFiles(savepath, FileUploadHost);
        FileUtil.GetFilesList(savepath, GVFileLists);

        //string sScript = "alert('資料新增成功!');window.location.href='../Announcement/AnnouncementDetail.aspx?mode=add';";
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "InsertOK", sScript, true);

        CommonUtil.DisplayInfoMessage("公告：" + txt_BULLETIN_ID.Text + " 新增成功!", lbl_Message);
        InsertMode();
        //清畫面
        txt_BULLETIN_ID.Text = DateTime.Now.ToString("yyyyMMdd-HHmmss-ff");
        txt_START_DATE.Text = DateTime.Now.ToString("yyyyMMdd");
        txt_END_DATE.Text = "";
        txt_BULLETIN_TEXT.Text = "";
        txt_BULLETIN_SPEC.Text = "";
        hidseq_C.Value = "";
        hidcompany_C.Value = "";
        hidbu_C.Value = "";
        hidzone_C.Value = "";
        hidarea_C.Value = "";
        hiddept_C.Value = "";
        hidcompany_C_T.Value = "";
        hidbu_C_T.Value = "";
        hidzone_C_T.Value = "";
        hidarea_C_T.Value = "";
        hiddept_C_T.Value = "";


        //Response.Redirect("~/Announcement/AnnouncementDetail.aspx?mode=add");
      }
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
      ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "btn_Insert_Click", ex.Message, "", "c#", "0", ex.StackTrace);
    }
  }
  protected void btn_Delete_Click(object sender, EventArgs e)
  {
    try
    {
        foreach (var item in ctx.KRJ_BULLETIN_CONDITION.Where(t => t.BULLETIN_ID == txt_BULLETIN_ID.Text))
        {
            var q = from o in ctx.KRJ_BULLETIN_CONDITION
                    where o.BULLETIN_ID == txt_BULLETIN_ID.Text && o.BULLETIN_SEQ == item.BULLETIN_SEQ
                    select o;
            if (q.FirstOrDefault() != null)
            {
                ctx.KRJ_BULLETIN_CONDITION.Remove(q.FirstOrDefault());
                //ctx.SaveChanges();
            }
        }

      //KRJEntities ctx = new KRJEntities();
        var BULLETIN_MASTER = ctx.KRJ_BULLETIN_MASTER
          .Where(b => b.BULLETIN_ID == txt_BULLETIN_ID.Text)
          .FirstOrDefault();
      if (BULLETIN_MASTER != null)
      {
          ctx.KRJ_BULLETIN_MASTER.Remove(BULLETIN_MASTER);
      }
      ctx.SaveChanges();
      Response.Redirect("~/Announcement/Announcement.aspx");
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
      ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "btn_Delete_Click", ex.Message, "", "c#", "0", ex.StackTrace);
    }
  }

  protected void GVFileLists_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      try
      {
          GridView gv = sender as GridView;
          string fullname = ((Label)gv.Rows[e.RowIndex].FindControl("lbl_FullName")).Text; // 檔案路徑
          string directoryName = ((Label)gv.Rows[e.RowIndex].FindControl("lbl_DirectoryName")).Text; ; //資料夾路徑
          if (System.IO.File.Exists(fullname))
          {
              System.IO.File.Delete(fullname);
          }
          System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(directoryName);
          gv.DataSource = d.GetFiles();
          gv.DataBind();
      }
      catch (Exception ex)
      {
          CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
          ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "GVFileLists_RowDeleting", ex.Message, "", "c#", "0", ex.StackTrace);
      }
  }
  protected void btn_Update_Click(object sender, EventArgs e)
  {
    try
    {
        

        char[] delimiterChars = { ',' };
        string[] seq = hidseq_C.Value.Split(delimiterChars);
        string[] company = hidcompany_C.Value.Split(delimiterChars);
        string[] bu = hidbu_C.Value.Split(delimiterChars);
        string[] zone = hidzone_C.Value.Split(delimiterChars);
        string[] area = hidarea_C.Value.Split(delimiterChars);
        string[] dept = hiddept_C.Value.Split(delimiterChars);
        string sSEQ = "";
        for (int i = 0; i < seq.Length - 1; i++)
        {
            sSEQ = seq[i].ToString();
            sSEQ = int.Parse(sSEQ).ToString("00");
            SAVE_Condition_S(txt_BULLETIN_ID.Text, sSEQ, company[i], bu[i], zone[i], area[i], dept[i], seq.Length - 1);
        }
        string BULLETIN_TEXT_TW = "", BULLETIN_TEXT_GB = "", BULLETIN_TEXT_EN = "";
        string BULLETIN_SPEC_TW = "", BULLETIN_SPEC_GB = "", BULLETIN_SPEC_EN = "";
        BULLETIN_TEXT_TW = HttpUtility.HtmlDecode(hid_BULLETIN_TEXT_TW.Value.Trim());
        BULLETIN_SPEC_TW = HttpUtility.HtmlDecode(hid_BULLETIN_SPEC_TW.Value.Trim());
        BULLETIN_TEXT_GB = HttpUtility.HtmlDecode(hid_BULLETIN_TEXT_GB.Value.Trim());
        BULLETIN_SPEC_GB = HttpUtility.HtmlDecode(hid_BULLETIN_SPEC_GB.Value.Trim());
        BULLETIN_TEXT_EN = HttpUtility.HtmlDecode(hid_BULLETIN_TEXT_EN.Value.Trim());
        BULLETIN_SPEC_EN = HttpUtility.HtmlDecode(hid_BULLETIN_SPEC_EN.Value.Trim());


      //新增EDM資料至DB
      //KRJEntities dc = new KRJEntities();
      var UPT_BULLETIN_MASTER = ctx.KRJ_BULLETIN_MASTER
         .Where(b => b.BULLETIN_ID == txt_BULLETIN_ID.Text)
         .FirstOrDefault();
      if (UPT_BULLETIN_MASTER != null)
      {
        //UPT_BULLETIN_MASTER.BULLETIN_ID = txt_BULLETIN_ID.Text;
        UPT_BULLETIN_MASTER.START_DATE = txt_START_DATE.Text;
        UPT_BULLETIN_MASTER.END_DATE = string.IsNullOrEmpty(txt_END_DATE.Text) ? "20201231" : txt_END_DATE.Text; //假設沒有給結束日期，則指定2020年        
        UPT_BULLETIN_MASTER.BULLETIN_TEXT_TW = BULLETIN_TEXT_TW;
        UPT_BULLETIN_MASTER.BULLETIN_SPEC_TW = BULLETIN_SPEC_TW;
        UPT_BULLETIN_MASTER.BULLETIN_TEXT_GB = BULLETIN_TEXT_GB;
        UPT_BULLETIN_MASTER.BULLETIN_SPEC_GB = BULLETIN_SPEC_GB;
        UPT_BULLETIN_MASTER.BULLETIN_TEXT_EN = BULLETIN_TEXT_EN;//txt_BULLETIN_TEXT_EN.Text;
        UPT_BULLETIN_MASTER.BULLETIN_SPEC_EN = BULLETIN_SPEC_EN;//txt_BULLETIN_SPEC_EN.Text.Trim();
        UPT_BULLETIN_MASTER.REMARK = txt_REMARK.Text;
        UPT_BULLETIN_MASTER.STATUS = ddl_Status.SelectedValue;

        UPT_BULLETIN_MASTER.UPD_ID = User.Identity.Name;
        UPT_BULLETIN_MASTER.UPD_DATE = DateTime.Now.ToString("yyyyMMdd");
        UPT_BULLETIN_MASTER.UPD_TIME = DateTime.Now.ToString("HHmmss");
      }
      ctx.SaveChanges();

      //當更新時，同步上傳檔案 FileUpload_Attachments            
      string savepath =
          Server.MapPath(ConfigurationManager.AppSettings["UploadFilePath"].ToString() + txt_BULLETIN_ID.Text);
      FileUtil.UploadMultiFiles(savepath, FileUploadHost);
      FileUtil.GetFilesList(savepath, GVFileLists);

      CommonUtil.DisplayInfoMessage("公告：" + txt_BULLETIN_ID.Text + " 更新成功!", lbl_Message);

      UpdateMode();

    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
      ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "btn_Update_Click", ex.Message, "", "c#", "0", ex.StackTrace);
    }
  }
  protected void GVFileLists_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      try
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              HyperLink link = (HyperLink)e.Row.FindControl("Link_Download");
              link.NavigateUrl = string.Format("~/Announcement/DownloadHandler.ashx?Fn={0}&Bid={1}",
                  e.Row.Cells[1].Text, txt_BULLETIN_ID.Text);
          }
      }
      catch (Exception ex)
      {
          CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
          ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "GVFileLists_RowDataBound", ex.Message, "", "c#", "0", ex.StackTrace);
      }

  }

  
 

  protected void ddl_so_DataBound(object sender, EventArgs e)
  {
    DropDownList ddl = sender as DropDownList;
    ddl.Items.Insert(0, new ListItem()
    {
      Text = "- All -",
      Value = "ALL"
    });
  }

  
  MISEntities mis = new MISEntities();
  protected void GridViewPermission_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      try
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              string name1 = e.Row.Cells[2].Text;
              if (!string.IsNullOrEmpty(name1) && name1 != "ALL")
              {
                  e.Row.Cells[2].Text = BLogical.GetDeptNameByID(name1) + "[" + name1 + "]";
              }
              string name2 = e.Row.Cells[3].Text;
              if (!string.IsNullOrEmpty(name2) && name2 != "ALL")
              {
                  e.Row.Cells[3].Text = BLogical.GetDeptNameByID(name2) + "[" + name2 + "]";
              }
              string name3 = e.Row.Cells[4].Text;
              if (!string.IsNullOrEmpty(name3) && name3 != "ALL")
              {
                  e.Row.Cells[4].Text = BLogical.GetDeptNameByID(name3) + "[" + name3 + "]";
              }
              string name4 = e.Row.Cells[5].Text;
              if (!string.IsNullOrEmpty(name4) && name4 != "ALL")
              {
                  e.Row.Cells[5].Text = BLogical.GetDeptNameByID(name4) + "[" + name4 + "]";
              }
              string name5 = e.Row.Cells[6].Text;
              if (!string.IsNullOrEmpty(name5) && name5 != "ALL")
              {
                  e.Row.Cells[6].Text = BLogical.GetDeptNameByID(name5) + "[" + name5 + "]";
              }
          }
      }
      catch (Exception ex)
      {
          CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
          ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "GridViewPermission_RowDataBound", ex.Message, "", "c#", "0", ex.StackTrace);
      }

  }

  protected DataSet DBINT(string DEPT_NO)
  {
      DataSet ds = null;
      try
      {
          dbAccess m_dba = new dbAccess();
          StringBuilder m_sb = new StringBuilder();
          ArrayList m_arrParam = new ArrayList();

          m_sb.Clear();
          m_sb.Append(" select A.*,B.DEPT_NAME from  ");
          m_sb.Append(" ( ");
          m_sb.Append(" select BI4101 ");
          m_sb.Append(" ,case when BI4101 = BI4111 then case when BI4112 is null then BI4113 else BI4112 end ");
          m_sb.Append(" when BI4101 = BI4112 then case when BI4113 is null then BI4114 else BI4113 end ");
          m_sb.Append(" when BI4101 = BI4113 then case when BI4114 is null then case when BI4115 is null then case when BI4116 is null then case when BI4117 is null then case when BI4118 is null then BI4119 else BI4118 end else BI4117 end else BI4116 end else BI4115 end else BI4114 end ");
          m_sb.Append(" when BI4101 = BI4114 then case when BI4115 is null then case when BI4116 is null then case when BI4117 is null then case when BI4118 is null then BI4119 else BI4118 end else BI4117 end else BI4116 end else BI4115 end ");
          m_sb.Append(" when BI4101 = BI4115 then case when BI4116 is null then case when BI4117 is null then case when BI4118 is null then BI4119 else BI4118 end else BI4117 end else BI4116 end ");
          m_sb.Append(" when BI4101 = BI4116 then case when BI4117 is null then BI4118 else BI4117 end "); //
          m_sb.Append(" when BI4101 = BI4117 then case when BI4118 is null then BI4119 else BI4118 end ");
          m_sb.Append(" when BI4101 = BI4118 then BI4119 "); //
          m_sb.Append(" when BI4101 = BI4119 then N'0000' ");
          m_sb.Append(" else  null "); //

          m_sb.Append(" end  PARENT_DEPT_ID "); //
          m_sb.Append(" from as400_bif41 "); //
          m_sb.Append(" ) A "); //
          m_sb.Append(" left join MIS_DEPT_AP B on A.BI4101 = B.DEPT_ID  "); //
         

          m_sb.Append(" Where 1=1");
          if (DEPT_NO != "")
          {
              m_sb.Append(" AND A.PARENT_DEPT_ID = :DEPT_NO ");
              m_arrParam.Add(m_dba.GetOracleParam(":DEPT_NO", DEPT_NO));
          }
          m_sb.Append(" ORDER by  A.PARENT_DEPT_ID "); //
          ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);


      }
      catch (Exception ex)
      {
          CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
          ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
      }
      return ds;
  }

  protected DataSet CONDITION(string BULLETIN_ID)
  {
      DataSet ds = null;
      try
      {
          dbAccess m_dba = new dbAccess();
          StringBuilder m_sb = new StringBuilder();
          ArrayList m_arrParam = new ArrayList();

          m_sb.Clear();
          m_sb.Append(" select H.BULLETIN_ID,H.BULLETIN_SEQ,");
          m_sb.Append(" H.NAME1,case when H.NAME1 = 'ALL' then N'-All-' else NVL(A.DEPT_NAME,' ') end  DEPT_NAME1, ");
          m_sb.Append(" H.NAME2,case when H.NAME2 = 'ALL' then N'-All-' else NVL(B.DEPT_NAME,' ') end  DEPT_NAME2, ");
          m_sb.Append(" H.NAME3,case when H.NAME3 = 'ALL' then N'-All-' else NVL(C.DEPT_NAME,' ') end  DEPT_NAME3, ");
          m_sb.Append(" H.NAME4,case when H.NAME4 = 'ALL' then N'-All-' else NVL(D.DEPT_NAME,' ') end  DEPT_NAME4, ");
          m_sb.Append(" H.NAME5,case when H.NAME5 = 'ALL' then N'-All-' else NVL(E.DEPT_NAME,' ') end  DEPT_NAME5  ");
          m_sb.Append(" from KRJ_BULLETIN_CONDITION H left join MIS_DEPT_AP A on H.NAME1 = A.DEPT_ID ");
          m_sb.Append(" left join MIS_DEPT_AP B on H.NAME2 = B.DEPT_ID ");
          m_sb.Append(" left join MIS_DEPT_AP C on H.NAME3 = C.DEPT_ID ");
          m_sb.Append(" left join MIS_DEPT_AP D on H.NAME4 = D.DEPT_ID ");
          m_sb.Append(" left join MIS_DEPT_AP E on H.NAME5 = E.DEPT_ID ");
          
          m_sb.Append(" where H.BULLETIN_ID = :BULLETIN_ID ");
          m_arrParam.Add(m_dba.GetOracleParam(":BULLETIN_ID", BULLETIN_ID));
          m_sb.Append(" ORDER by  H.BULLETIN_ID,H.BULLETIN_SEQ "); //
          ds = m_dba.runExecuteQuery("CONDITION", m_sb.ToString(), m_arrParam);


      }
      catch (Exception ex)
      {
          CommonUtil.DisplayErrorMessage(ex.Message, lbl_Message);
          ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "CONDITION", ex.Message, "", "c#", "0", ex.StackTrace);
      }
      return ds;
  }
  
  protected string SAVE_Condition_S(string BULLETIN_ID, string SEQ, string Company, string bu, string zone, string AREA, string Dept,int count)
  {
      try
      {
          
          //刪除CONDITION
          if (SEQ == "01")
          {
              
                foreach (var item in ctx.KRJ_BULLETIN_CONDITION.Where(t => t.BULLETIN_ID == BULLETIN_ID))
                {
                    var q = from o in ctx.KRJ_BULLETIN_CONDITION
                            where o.BULLETIN_ID == BULLETIN_ID && o.BULLETIN_SEQ == item.BULLETIN_SEQ
                            select o;
                    if (q.FirstOrDefault() != null)
                    {
                        ctx.KRJ_BULLETIN_CONDITION.Remove(q.FirstOrDefault());
                        //ctx.SaveChanges();
                    }
                }

             
          }
          //新增條件
          KRJ_BULLETIN_CONDITION new_condition = new KRJ_BULLETIN_CONDITION();
          new_condition.BULLETIN_ID = BULLETIN_ID;
          new_condition.BULLETIN_SEQ = SEQ;
          //new_condition.NAME1 = ddlcompany.SelectedValue;
          new_condition.NAME1 = Company;
          new_condition.NAME2 = bu;
          new_condition.NAME3 = zone;
          new_condition.NAME4 = AREA;
          new_condition.NAME5 = Dept;

          new_condition.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
          new_condition.CRT_TIME = DateTime.Now.ToString("HHmmss");
          new_condition.CRT_ID = User.Identity.Name;
          ctx.KRJ_BULLETIN_CONDITION.Add(new_condition);
          if (count.ToString("00") == SEQ)
          {
              //ctx.SaveChanges();
          }
          return "";
      }
      catch (Exception ex)
      {
          ERROR_DBSAVE("AnnouncementDetail.aspx.cs", "SAVE_Condition_S", ex.Message, "", "c#", "0", ex.StackTrace);
          return ex.Message;
      }

  }
  protected string GetControlClientId()
  {
           return txt_BULLETIN_SPEC.ClientID;
  }

}

